package com.guodi.model.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.AjaxResult;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.model.constant.CommonConst;
import com.guodi.model.entity.MdlFactor;
import com.guodi.model.entity.MdlModel;
import com.guodi.model.service.IMdlFactorService;
import com.guodi.model.service.IMdlModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;


/**
 * @描述：模型因子表控制类
 * @作者: 代鹏刚
 * @日期: 2020-06-17
 */
@RestController
@RequestMapping("/MdlFactor")
@Api(value = "API - MdlFactorController", description = "模型因子表")
public class MdlFactorController extends BaseController {

    @Resource
    public IMdlFactorService mdlFactorService;
    @Resource
    private IMdlModelService mdlModelService;

    /**
    * @描述: 根据实体属性分页查询
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020-06-17
    **/
    @PostMapping("/listByEntity")
    @ApiOperation(value = " 根据实体属性分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", required = true, dataType = "long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示数量", required = true, dataType = "long", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "keyWord", value = "关键字搜索", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sortType", value = "排序类型(1:时间 2:名称)", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sortMethod", value = "排序方式(1:向下 2:向上)", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult listByEntity(@ApiIgnore Page page, @ApiIgnore MdlFactor mdlFactor, @ApiIgnore CommonConst commonConst){
        IPage<MdlFactor> mdlFactors = mdlFactorService.listByEntity(page,mdlFactor,commonConst);
        return new AjaxResult(mdlFactors);
    }

    /**
     * @描述: 根据主键查询
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-06-17
     **/
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public AjaxResult findById(String id) {
        if(StringUtils.isBlank(id)){
            return new AjaxResult(StatusCode.ERROR_CODE_40005,"id不能为空");
        }
        MdlFactor mdlFactor = mdlFactorService.getById(id);
        return new AjaxResult(mdlFactor);
    }

    /**
     * @描述: 新增
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-06-17
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "因子名称", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "因子数据类型", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "modelId", value = "关联模型ID", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "modelName", value = "关联模型名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "purpose", value = "因子用途",  dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "layerId", value = "关联图层ID（多个图层ID逗号隔开）",  dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "analysisModelId", value = "关联分析模型ID（多个模型ID逗号隔开）",  dataType = "string", paramType = "query")
    })
    public AjaxResult save(@ApiIgnore MdlFactor mdlFactor){
        prepareData(mdlFactor, true);
        mdlFactor.setId(UuidUtil.get32UUID());
        mdlFactorService.save(mdlFactor);
        return new AjaxResult();
    }

    /**
     * @描述: 修改
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-06-17
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "因子名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "因子数据类型", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "modelId", value = "关联模型ID", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "modelName", value = "关联模型名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "purpose", value = "因子用途",  dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "analysisModelId", value = "关联分析模型ID（多个模型ID逗号隔开）",  dataType = "string", paramType = "query")
    })
    public AjaxResult update(@ApiIgnore MdlFactor mdlFactor) {
        prepareData(mdlFactor, false);
        mdlFactorService.updateById(mdlFactor);
        return new AjaxResult();
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-06-17
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "多个使用逗号隔开", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult delete(String ids){
        String[] id = ids.split(",");
        mdlFactorService.removeByIds(Arrays.asList(id));
        return new AjaxResult();
    }

    /**
    * @描述: 因子分析-查询关联模型列表数据
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/6/17 17:13
    **/
    @PostMapping("/listModelByModelId")
    @ApiOperation(value = "查询关联模型列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "analysisModelId", value = "关联分析模型ID（多个模型ID逗号隔开）",required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "模型名称", dataType = "string", paramType = "query")
    })
    public AjaxResult listModelByModelId(String analysisModelId, String name){
        Map resultMap = new HashMap();
        List<MdlModel> modelList = mdlModelService.listModelByModelId(analysisModelId,name);
        List<MdlModel> analysisModel = new ArrayList<>();
        if(StringUtils.isNotBlank(analysisModelId)){
            String[] modelIds = analysisModelId.split(",");
            analysisModel = (List<MdlModel>)mdlModelService.listByIds(Arrays.asList(modelIds));
        }
        resultMap.put("modelList",modelList);
        resultMap.put("analysisModel",analysisModel);
        return new AjaxResult(resultMap);
    }

    /**
    * @描述: 获取关联模型的因子列表数据
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/6/19 10:23
    **/
    @PostMapping("/listFactorByModelId")
    @ApiOperation(value = "获取关联模型的因子列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "模型ID", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult listFactorByModelId(String modelId){
        Map map = new HashMap();
        map.put("model_id",modelId);
        List<MdlFactor> mdlFactors = (List<MdlFactor>)mdlFactorService.listByMap(map);
        return new AjaxResult(mdlFactors);
    }

    private void prepareData(MdlFactor mdlFactor, boolean isSave) {
        if(isSave){
            mdlFactor.setCreator(this.getUser().getUserId().toString());
            mdlFactor.setCreateTime(new Date());
            //防止误修改
            mdlFactor.setModifier(null);
            mdlFactor.setModifyTime(null);
        }else{
            mdlFactor.setModifier(this.getUser().getUserId().toString());
            mdlFactor.setModifyTime(new Date());
            //防止误修改
            mdlFactor.setCreator(null);
            mdlFactor.setCreateTime(null);
        }
    }
}
